{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-11-18T10:31:52.217398Z",
     "start_time": "2025-11-18T10:31:51.939591Z"
    }
   },
   "source": [
    "import json\n",
    "# Python 模块化\n",
    "from openai import OpenAI\n",
    "# python 实例化不需要new\n",
    "client=OpenAI(\n",
    "    api_key='sk-e1068fd3f1e34115bdb046f14be13baa',\n",
    "    base_url='https://api.deepseek.com/v1'\n",
    ")\n",
    "def send_messages(messages):\n",
    "    response=client.chat.completions.create(\n",
    "        model='deepseek-reasoner',\n",
    "        messages=messages,\n",
    "#         告诉大模型可以调用tools\n",
    "        tools=tools,\n",
    "#         让大模型自由选择\n",
    "        tool_choice='auto'\n",
    "    )\n",
    "    return response\n",
    "# openai 定义的tool 接口标准\n",
    "tools = [\n",
    "    # 创建tool  JSON申明\n",
    "    {\n",
    "        # tool 就是一个函数\n",
    "        \"type\": \"function\",\n",
    "        \"function\": {\n",
    "            \"name\": \"get_closing_price\",\n",
    "            # 自然语言处理\n",
    "            \"description\": \"获取指定股票的收盘价\",\n",
    "            \"parameters\": {\n",
    "                \"type\": \"object\",\n",
    "                \"properties\": {\n",
    "                    \"name\": {\n",
    "                        \"type\": \"string\",\n",
    "                        \"description\": \"股票名称\"\n",
    "                    }\n",
    "                },\n",
    "                \"required\": [\"name\"]\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "]\n",
    "def  get_closing_price(name):\n",
    "        if name =='青岛啤酒':\n",
    "            return '5555'\n",
    "        else:\n",
    "            return '未找到该股票'\n",
    "messages=[{'role':'user','content':'青岛啤酒的收盘价是多少？'}]\n",
    "response= send_messages(messages)\n",
    "message=response.choices[0].message\n",
    "print(message)\n",
    "ChatCompletionMessage(content='我来帮您查询青岛啤酒的收盘价。', refusal=None, role='assistant', annotations=None, audio=None, function_call=None, tool_calls=[ChatCompletionMessageFunctionToolCall(id='call_00_1yN8O6igyHYL6L69lB0C3tjd', function=Function(arguments='{\"name\": \"青岛啤酒\"}', name='get_closing_price'), type='function', index=0)])"
   ],
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'openai'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mModuleNotFoundError\u001B[39m                       Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[1]\u001B[39m\u001B[32m, line 3\u001B[39m\n\u001B[32m      1\u001B[39m \u001B[38;5;28;01mimport\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[34;01mjson\u001B[39;00m\n\u001B[32m      2\u001B[39m \u001B[38;5;66;03m# Python 模块化\u001B[39;00m\n\u001B[32m----> \u001B[39m\u001B[32m3\u001B[39m \u001B[38;5;28;01mfrom\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[34;01mopenai\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;28;01mimport\u001B[39;00m OpenAI\n\u001B[32m      4\u001B[39m \u001B[38;5;66;03m# python 实例化不需要new\u001B[39;00m\n\u001B[32m      5\u001B[39m client=OpenAI(\n\u001B[32m      6\u001B[39m     api_key=\u001B[33m'\u001B[39m\u001B[33msk-e1068fd3f1e34115bdb046f14be13baa\u001B[39m\u001B[33m'\u001B[39m,\n\u001B[32m      7\u001B[39m     base_url=\u001B[33m'\u001B[39m\u001B[33mhttps://api.deepseek.com/v1\u001B[39m\u001B[33m'\u001B[39m\n\u001B[32m      8\u001B[39m )\n",
      "\u001B[31mModuleNotFoundError\u001B[39m: No module named 'openai'"
     ]
    }
   ],
   "execution_count": 1
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
